package net.sppan.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import net.sppan.base.config.httpConfig.HttpAPIService;
import net.sppan.base.dao.IExuserContentDao;
import net.sppan.base.dao.support.IBaseDao;
import net.sppan.base.entity.ExContentView;
import net.sppan.base.entity.ExuserContent;
import net.sppan.base.entity.UsertTask;
import net.sppan.base.service.IExuserContentService;
import net.sppan.base.service.support.impl.BaseServiceImpl;

import org.springframework.data.domain.Pageable;

import com.alibaba.fastjson.JSON;

@Service
public class ExuserContentServiceImpl extends BaseServiceImpl<ExuserContent,Long> implements IExuserContentService{
  
	@Value("${DY.EX.HTTPURL}")
	private String httpUrl;
	
	@Value("${DY.EX.TOKEN}")
	private String token;
	
	@Autowired
	private IExuserContentDao exuserContentDao;

	@Autowired
	private HttpAPIService apiService;
	
	@Override
	public IBaseDao<ExuserContent, Long> getBaseDao() {
		return this.exuserContentDao;
	}
	@Override
	public Page<ExContentView>  findExuserContentList(String userId,String type,  String status,Pageable pageable){
		try {
			Page<ExContentView>  page=exuserContentDao.findExuserContentList(Long.valueOf(userId), status, pageable);
			return page;
		} catch (Exception e) {
			return null;
		} 
	}
	@Override
	public void updateExuserContent(ExuserContent exuserContent) {
		if(exuserContent !=null){
			ExuserContent dbExuserContent=find(exuserContent.getId());
			dbExuserContent.setId(exuserContent.getId());
			dbExuserContent.setExDesc(exuserContent.getExDesc());
			dbExuserContent.setExResult(exuserContent.getExResult());
			dbExuserContent.setExTime(exuserContent.getExTime());
	        update(dbExuserContent);
	        postExResultHttp(dbExuserContent.getDyContentId(), dbExuserContent.getExResult());
		}
	}
	@Override
	public List<UsertTask> findExuserContentList(String status) {
		List<Object[]> list =exuserContentDao.findExuserContentList(status);
		List<UsertTask> usertTasks =new ArrayList<UsertTask>();
		if(!list.isEmpty()&&list.size()>0){
			for(Object[] objects:list){
				usertTasks.add(new UsertTask(Long.valueOf(String.valueOf(objects[0])), Long.valueOf(String.valueOf(objects[1]==null?0:objects[1]))));
			}
		}
		return usertTasks;
	}
	@Override
	public void inserList(List<ExuserContent> contents){
		exuserContentDao.saveAll(contents);
	}

	@Async
	public  void  postExResultHttp(Long dyContentId ,String exResult){
		//1qaz@WSX1234QWER
		Map<String, Object> params =new HashMap<String, Object>();
		params.put("dyID", 3991234);
		params.put("type", 4);
		params.put("operator", Integer.parseInt(exResult));
		List<Integer> dyDataID =new ArrayList<Integer>();
		dyDataID.add(dyContentId.intValue());
		params.put("dyDataID", dyDataID);
		params.put("token", token);
		params.put("deviceID",String.valueOf(System.currentTimeMillis()));
		String vss = null;
		try {
			vss = apiService.doPost(httpUrl, JSON.toJSONString(params));
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(vss);
	}
	@Override
	public Page<ExContentView> findExuserContentListHis(Pageable pageable) {
		return exuserContentDao.findExuserContentListHis(pageable);
	}
}
